package web.weibo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import web.weibo.mapper.MapperWeibo;
import web.weibo.model.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class ServiceWeibo {

    @Autowired
    private MapperWeibo mapperWeibo;
    @Autowired
    private PlatformTransactionManager transactionManager;

    public static String formattedTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(date);
    }

    public Weibo weiboNow(int weiboId) {
        Weibo weibo = this.mapperWeibo.weiboNow(weiboId);
        for (int i = 0; i < weibo.comments.size(); i++) {
            Comment comment = weibo.comments.get(i);
            int commentUserId = comment.getUserId();
            User user = this.mapperWeibo.userComment(commentUserId);
            comment.setCommentUser(user);
        }
        return weibo;
    }

    private ArrayList<Weibo> getCommentUser(ArrayList<Weibo> weibos) {
        for (int i = 0; i < weibos.size(); i++) {
            Weibo weibo = weibos.get(i);
            int countComments = this.mapperWeibo.countComment(weibo.getId());
            weibo.setCountComments(countComments);
            for (int j = 0; j < weibo.comments.size(); j++) {
                Comment comment = weibo.comments.get(j);
                int commentUserId = comment.getUserId();
                User user = this.mapperWeibo.userComment(commentUserId);
                comment.setCommentUser(user);
            }
        }
        return weibos;
    }

    public ArrayList<Weibo> allWeibos() {
        ArrayList<Weibo> weibos = this.mapperWeibo.allWeibosWithComments();
        return getCommentUser(weibos);
    }

    public ArrayList<Weibo> currentUserWeibos(int userId) {
        ArrayList<Weibo> weibos = this.mapperWeibo.currentUserWeibosWithComments(userId);
        return getCommentUser(weibos);
    }

    public Comment comment(int commentId) {
        Comment comment = this.mapperWeibo.comment(commentId);
        return comment;
    }

    public int countWeibos(int userId) {
        int countWeibos = this.mapperWeibo.currentWeibos(userId);
        return countWeibos;
    }

    public int countComment(int weiboId) {
        int countComment = this.mapperWeibo.countComment(weiboId);
        return countComment;
    }

    public int countCares(int userId) {
        int countCares = this.mapperWeibo.countCares(userId);
        return countCares;
    }

    public int countBeCares(int userId) {
        int countBeCares = this.mapperWeibo.countBeCares(userId);
        return countBeCares;
    }

    public int myAllComments(int userId) {
        int myAllComments = this.mapperWeibo.myAllComments(userId);
        return myAllComments;
    }

    public int iscare(int currentUserId, int userId) {
        int iscare = this.mapperWeibo.iscare(currentUserId, userId);
        return iscare;
    }

    public User userWeibo(int userId) {
        User userWeibo = this.mapperWeibo.userWeibo(userId);
        return userWeibo;
    }

    public ArrayList<Weibo> myComments(int userId) {
        ArrayList<Weibo> myComments = this.mapperWeibo.myComment(userId);
        return myComments;
    }

    public ArrayList<User> timelineWeibos(User currentUser) {
        ArrayList<User> followees = this.mapperWeibo.followUser(currentUser.id);
        return followees;
    }

    public ArrayList<User> beTimelineWeibos(User currentUser) {
        ArrayList<User> followers = this.mapperWeibo.beFollowUser(currentUser.id);
        return followers;
    }

    public void follow(int currentUserId, int userId) {
        this.mapperWeibo.follow(currentUserId, userId);
    }

    public void followOver(int currentUserId, int userId) {
        this.mapperWeibo.followOver(currentUserId, userId);
    }

    public void overFans(int currentUserId, int userId) {
        this.mapperWeibo.overFans(currentUserId, userId);
    }

    public void add(String content, User currentUser) {
        // String createdTime = formattedTime();
        // this.mapperWeibo.weiboAdd(content, createdTime, currentUser.id);
        // String key = "countWeibo";
        // this.redisTemplate.delete(key);

        String createdTime = formattedTime();
        TransactionStatus txStatus =
                transactionManager.getTransaction(new DefaultTransactionDefinition());
        try {
            mapperWeibo.weiboAdd(content, createdTime, currentUser.id);
            // throw new RuntimeException("测试回滚");
        } catch (Exception e) {
            transactionManager.rollback(txStatus);
            throw e;
        }
        transactionManager.commit(txStatus);
    }

    public void commentAdd(String content, User currentUser, int weiboId) {
        String createdTime = formattedTime();
        this.mapperWeibo.commentAdd(content, createdTime, currentUser.id, weiboId);
    }

    public void delete(int id, User currentUser) {
        this.mapperWeibo.weiboDelete(id, currentUser.id);
    }

    public void commentDelete(int id, User currentUser) {
        this.mapperWeibo.commentDelete(id, currentUser.id);
    }

    public void update(int id, String content, User currentUser) {
        this.mapperWeibo.weiboUpdate(id, content, currentUser.id);
    }

    public void commentUpdate(int id, String content, User currentUser) {
        this.mapperWeibo.commentUpdate(id, content, currentUser.id);
    }
}
